#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

void MergeSort(int* a, int left, int right,int* tmp)
{
	if (left >= right)
	{
		return;
	}
	int mid = left + (right - left) / 2;
	MergeSort(a, left, mid, tmp);
	MergeSort(a, mid+1, right, tmp);
	int begin1 = left,end1 = mid;
	int begin2 = mid + 1, end2 = right;
	int i = left;
	while (begin1 <= end1 && begin2 <= end2)
	{
		if (a[begin2] < a[begin1])
		{
			tmp[i++] = a[begin2++];
		}
		else
		{
			tmp[i++] = a[begin1++];
		}
	}
	while (begin1 <= end1)
	{
		tmp[i++] = a[begin1++];
	}
	while (begin2 <= end2)
	{
		tmp[i++] = a[begin2++];
	}
	for (int j = left; j <= right; j++)
	{
		a[j] = tmp[j];
	}
}


int main()
{
	//int* a = (int*)malloc(sizeof(int) * 10000);
	srand((unsigned)time(NULL));
	int n = 10000000;
	int* a = (int*)malloc(sizeof(int) * n);
	for (int i = 0; i < n; i++)
	{
		a[i] = rand() % 10000000;
	}
	int* b = (int*)malloc(sizeof(int)*n);
	int begin = clock();
	MergeSort(a, 0, n-1, b );
	int end = clock();
	printf("%d ms\n",  end-begin);
	return 0;
}